package com.bdqn.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.crm.pojo.CstLost;
import com.bdqn.crm.pojo.SalChance;
import com.bdqn.crm.service.CstLostService;
import com.bdqn.crm.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/lost")
public class CstLostController {

    @Resource
    private CstLostService cstLostService;


    @RequestMapping("/list")
    public String list(HttpServletRequest request, Model model, @RequestParam(value = "lstCustName",required = false) String custName, @RequestParam(value = "lstCustManagerName",required = false) String CustManagerName, @RequestParam(required = false, defaultValue = "1") int pageIndex){
        Page<CstLost> page = new Page<>(pageIndex, 5);
        QueryWrapper<CstLost> queryWrapper = new QueryWrapper<>();
        //判断是否有查询条件
        if (custName != null && custName.trim() != "") {
            queryWrapper.like("lst_cust_name", custName);      //模糊设置条件
        }
        if (CustManagerName != null && CustManagerName.trim() != "") {
            queryWrapper.like("lst_cust_manager_name", CustManagerName);
        }
        if (cstLostService.count(queryWrapper) == 0) {     //此操作说明 数据库没有数据可展示，那么就全部清零
            page.setTotalElements(0);
            page.setContent(null);
        } else {
            page.setTotalElements(cstLostService.count(queryWrapper));
            page.setContent(cstLostService.selectPages(custName, CustManagerName, (page.getNumber() - 1) * page.getPageSize(), page.getPageSize()));
        }
        model.addAttribute("lstCustName",custName);
        model.addAttribute("lstCustManagerName",CustManagerName);
        model.addAttribute("rolePage", page);
        return "lost/list";
    }

    @RequestMapping("/edit")
    public String edit(Long lstId,Model model){
        QueryWrapper<CstLost> qw = new QueryWrapper<>();
        qw.eq("lst_id",lstId);
        CstLost cstLost = cstLostService.getOne(qw);
        model.addAttribute("losts",cstLost);
        return "lost/edit";
    }

    @RequestMapping("/save")
    public String save(CstLost cstLost){
        if(cstLostService.saveOrUpdate(cstLost)){
            return "redirect:/lost/list";
        }else {
            return "lost/edit";
        }
    }

}
